package model.map;

import utility.load_save.SaveFileWriter;
import model.core.GameScheduler;
import model.core.Notifiable;

/**
 * 
 * @author Zachary
 */
public class MapSaveObserver implements Notifiable{
	private GameMap map;
	/**
	 * Constructor for the GameStateObserver.
	 * @param gameMap
	 */
	public MapSaveObserver(GameMap map, int autoSavePeriod){
		this.map = map;
		GameScheduler.getInstance().register(autoSavePeriod, this);
	}
	
	/**
	 * This method causes the game drawing to occur.
	 */
	public void execute() {
		save();
	}
	
	/**
	 * Saves the map to file.
	 */
	public void save(){
		MapPoint boundary = this.map.getBoundary();
		SaveMap saveMap = new SaveMap(boundary.getXCoordinate(),boundary.getYCoordinate());
		SaveVisitor visitor = new SaveVisitor(saveMap);
		map.acceptMapVisitor(visitor);
		StringBuilder builder = new StringBuilder();
		builder.append(saveMap.saveFile());
		builder.append(visitor.addShroudedData());
		SaveFileWriter.writeToFile(builder.toString());
	}
}
